package us.versus.them.b3ge.character;

import flash.display.BitmapData;
import flash.geom.Point;

class Action {
	public var bitmapData:BitmapData;
	public var offsets:Array< Point >;

	public static var POINT_ZERO = new Point( 0,0 );

	public function new( bitmapData:BitmapData, offsets:Array< Point > ) {
		this.bitmapData = bitmapData;
		this.offsets = offsets;
	}

	public function offset( direction:Int ) {
		return if ( null == this.offsets || direction < 0 || direction >= offsets.length ) {
			POINT_ZERO;
		} else {
			this.offsets[ direction ];
		}
	}

	public function towards( current:Point, goal:Point ) {
		return this.closest( new Point( goal.x - current.x, goal.y - current.y ) );
	}

	public function closest( diff:Point ) {
		var closest = 0;
		var min = 0.0;
		var idx = 0;
		if ( null != this.offsets ) {
			for ( offset in this.offsets ) {
				var dist = Point.distance( offset, diff );
				if ( 0 == idx || dist < min ) {
					min = dist;
					closest = idx;
				}
				idx++;
			}
		}
		return closest;
	}

}
